package com.wc5818.wcWorkInfo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wc5818.common.AjaxResult;
import com.wc5818.wcBasicInfo.entity.WcBasicInfo;
import com.wc5818.wcBasicInfo.mapper.WcBasicInfoMapper;
import com.wc5818.wcComOrder.entity.WcComOrder;
import com.wc5818.wcComOrder.mapper.WcComOrderMapper;
import com.wc5818.wcWorkInfo.entity.WcWorkInfo;
import com.wc5818.wcWorkInfo.mapper.WcWorkInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/wcWorkInfo")
public class WcWorkInfoController {
    @Autowired
    WcWorkInfoMapper wcWorkInfoMapper;
    @Autowired
    WcComOrderMapper wcComOrderMapper;

    @PostMapping("/addOne")
    public Object insertOne(@RequestBody WcWorkInfo wcWorkInfo) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        if(wcWorkInfo.getId() == null) {
            wcWorkInfo.setCreatdate(LocalDateTime.now());
            wcWorkInfo.setUpdates(LocalDateTime.now());
            if(wcWorkInfoMapper.insert(wcWorkInfo) > 0)
                ajaxResult.setSuccess(true);

        } else {
            wcWorkInfo.setUpdates(LocalDateTime.now());
            if(wcWorkInfoMapper.updateById(wcWorkInfo) >= 0)
                ajaxResult.setSuccess(true);
        }

        return ajaxResult;
    }

    @GetMapping("queryByOrderNum")
    public Object queryByOrderNum(String orderId) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        List list = wcWorkInfoMapper.selectList(new QueryWrapper<WcWorkInfo>().eq("orderId", orderId));
        if(list != null && list.size() > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setData(list.get(0));
        }

        return ajaxResult;
    }

}
